package one.stand.util;

import one.stand.advice.ResultEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * Created by chengjiarui on 2019-06-26.
 */
public class WarnUtil {

    private final static ResultEnum resultEnum = ResultEnum.PARAM_CHECK;

    public static void isNull(Object data, String message) {
        isTrue(Objects.isNull(data), message, resultEnum.getCode());
    }

    public static void isNull(Object data, ResultEnum resultEnum) {
        isTrue(Objects.isNull(data), resultEnum.getMessage(), resultEnum.getCode());
    }

    public static void nonNull(Object data, String message) {
        nonNull(data, message, resultEnum.getCode());
    }

    public static void nonNull(Object data, ResultEnum resultEnum) {
        nonNull(data, resultEnum.getMessage(), resultEnum.getCode());
    }

    public static void nonNull(Object data, String message, int code) {
        isTrue(Objects.nonNull(data), message, code);
    }

    public static void nonNull(String data, String message) {
        nonNull(data, message, resultEnum.getCode());
    }

    public static void nonNull(String data, ResultEnum resultEnum) {
        nonNull(data, resultEnum.getMessage(), resultEnum.getCode());
    }

    public static void nonNull(String data, String message, int code) {
        isTrue(StringUtils.isNoneBlank(data), message, code);
    }

    public static void isFalse(boolean expression, String message) {
        isTrue(!expression, message, resultEnum.getCode());
    }

    public static void isFalse(boolean expression, ResultEnum resultEnum) {
        isTrue(!expression, resultEnum.getMessage(), resultEnum.getCode());
    }

    public static void fail() {
        isTrue(false, resultEnum);
    }

    public static void fail(String message) {
        isTrue(false, message);
    }

    public static void isTrue(boolean expression, String message) {
        isTrue(expression, message, resultEnum.getCode());
    }

    public static void isTrue(boolean expression, ResultEnum resultEnum) {
        isTrue(expression, resultEnum.getMessage(), resultEnum.getCode());
    }

    public static void isTrue(boolean expression, String message, int code) {
        if (!expression) {
            throw new OneIllegalArgumentException(message, code);
        }
    }
}
